package com.ruoyi.project.dict.material.service.impl;


import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.JsonUtil;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.framework.aspectj.lang.annotation.DataSource;
import com.ruoyi.framework.aspectj.lang.enums.DataSourceType;
import com.ruoyi.project.dict.material.domain.Material;
import com.ruoyi.project.dict.material.mapper.MaterialMapper;
import com.ruoyi.project.dict.material.service.IMaterialService;
import com.ruoyi.project.dict.materialTopicAsso.domain.MaterialTopicAsso;
import com.ruoyi.project.dict.materialTopicAsso.mapper.MaterialTopicAssoMapper;
import com.ruoyi.project.dict.res.domain.MaterialRes;
import com.ruoyi.project.dict.res.mapper.MaterialResMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 学习资料Service业务层处理
 *
 * @author shixiangwen
 * @date 2020-05-08
 */
@Service
@DataSource(DataSourceType.DICT)
public class MaterialServiceImpl implements IMaterialService {

    @Autowired
    private MaterialMapper materialMapper;

    @Autowired
    private MaterialResMapper materialResMapper;

    @Autowired
    private MaterialTopicAssoMapper materialTopicAssoMapper;


    /**
     * 查询学习资料
     *
     * @param id 学习资料ID
     * @return 学习资料
     */
    @Override

    public Material selectMaterialById(Long id) {
        return materialMapper.selectMaterialById(id);
    }

    /**
     * 查询学习资料列表
     *
     * @param material 学习资料
     * @return 学习资料
     */
    @Override
    public List<Material> selectMaterialList(Material material) {
        List<Material> list = materialMapper.selectMaterialList(material);
        return list;
    }

    /**
     * 新增学习资料
     *
     * @param material 学习资料
     * @return 结果
     */
    @Override
    public int insertMaterial(Material material) {

        Long id = getMaxId();
        material.setId(id);

        material.setTag(JsonUtil.toJson(material.getTags()));

        return materialMapper.insertMaterial(material);
    }

    private Long getMaxId() {
        Long id = materialMapper.selectMaxId();
        if (id == null) {
            id = 1L;
        } else {
            id += 1;
        }
        return id;
    }

    /**
     * 修改学习资料
     *
     * @param material 学习资料
     * @return 结果
     */
    @Override
    public int updateMaterial(Material material) {
        material.setTag(JsonUtil.toJson(material.getTags()));
        return materialMapper.updateMaterial(material);
    }

    /**
     * 删除学习资料对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteMaterialByIds(String ids) {

        String[] strIds = Convert.toStrArray(ids);
        for (String strId : strIds) {
            long id = Long.parseLong(strId);
            int materialResCount = materialResMapper.selectMaterialResCount(id);
            if (materialResCount > 0) {
                throw new BusinessException("请先清空当前资料下的资源后再删除");
            }
        }

        return materialMapper.deleteMaterialByIds(strIds);
    }

    @Override
    public int approveMaterialByIds(String ids, int approveSt) {


        String[] strIds = Convert.toStrArray(ids);
        int count = 0;
        for (String strId : strIds) {
            long id = Long.parseLong(strId);
            Material material = materialMapper.selectMaterialById(id);
            if (material.getApproveSt() == approveSt) {
                continue;
            }
            material.setApproveSt(approveSt);
            materialMapper.updateMaterial(material);
            count += 1;
        }

        return count > 0 ? 1 : 0;
    }

    /**
     * 删除学习资料信息
     *
     * @param id 学习资料ID
     * @return 结果
     */
    @Override
    public int deleteMaterialById(Long id) {
        return materialMapper.deleteMaterialById(id);
    }


    /**
     * 添加
     *
     * @param material
     * @param materialResList
     * @return
     */
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean add(Material material, List<MaterialRes> materialResList) {

        long id = getMaxId();
        material.setId(id);
        material.setCreatedTime(DateUtils.getNowDate());

        materialMapper.insertMaterial(material);

        materialResList.forEach(item -> {
            item.setMaterialId(id);
            materialResMapper.insertMaterialRes(item);
        });

        return true;
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean addMaterialToTopic(Long materialTopicId, Long[] materialIds) {

        for (long materialId : materialIds) {
            MaterialTopicAsso query = new MaterialTopicAsso();
            query.setMaterialId(materialId);
            query.setTopicId(materialTopicId);
            List<MaterialTopicAsso> olds = materialTopicAssoMapper.selectMaterialTopicAssoList(query);
            if (!olds.isEmpty()) {
                continue;
            }
            MaterialTopicAsso materialTopicAsso = new MaterialTopicAsso();
            materialTopicAsso.setTopicId(materialTopicId);
            materialTopicAsso.setMaterialId(materialId);
            materialTopicAsso.setCreatedTime(DateUtils.getNowDate());
            materialTopicAssoMapper.insertMaterialTopicAsso(materialTopicAsso);
        }

        return true;
    }
}
